
	use "$root/dat/SN_2020_Covid19.dta", clear
	
	*** Survey parameters
	
	gen nb_reached=1
	replace duration=duration/60
	replace end01=0 if end01<0 // some missing values for recall because of people stopping the call in the middle. Maybe we should assume that they don't want to be recalled. 

	collapse (sum) nb_reached (mean) consent end01 orange (p50) med_length=duration (p25) p25_length=duration (p75) p75_length=duration
	
	foreach var of varlist consent end01 orange{
	replace `var'=`var'*100
	}
	
	tostring nb_reached- p75_length, replace format(%5.0g) force
	sxpose, clear
	gen Indicator="Number reached" if _n==1
	replace Indicator="Acceptance rate" if _n==2
	replace Indicator="Accept recall rate" if _n==3
	replace Indicator="Share with Orange" if _n==4
	replace Indicator="Median length interview in minutes" if _n==5
	replace Indicator="25th percentile length interview in minutes" if _n==6
	replace Indicator="75th percentile length interview in minutes" if _n==7

	ren _var1 var1
	order Indicator var1
	
		texsave Indicator var1 using "$root/tex/table1",nonames replace frag title ("Survey parameters") nofix 
	
	*** Representativeness of sample
	
	use "$root/dat/SN_2020_Covid19.dta", clear
	drop if consent==0
	drop if pilot==1
	
	forvalue i=1/14{
	gen region`i'=(mi01==`i')
	}
	
	replace ls03=80 if ls03>80 & ls03!=. // Census of Senegal put together people aged 80+
	gen age_male=ls03 if ls02==0
	gen age_female=ls03 if ls02==1
	tab ls04 if ls02==0 & ls04>0 , gen(educ_male)
	tab ls04 if ls02==1 & ls04>0 , gen(educ_female)
	gen capital=(location2==1 & location2!=.)
	
	foreach var of varlist ls02 educ_male* educ_female* region1-region14 capital pov*{
	replace `var'=`var'*100
	}
	
	*Unstratified and unweighted
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui mean `var' 
	local b`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui mean `var' 
	local b`i'=_b[`var']
	local i=`i'+1
	}	
	
	local b_se=0
	forvalue i=1/11{
	local b_se=`se`i''+`b_se'
	}
	local b_se=`b_se'/11
	
	* Stratified
	
	svyset _n , poststrata(mi01) postweight(region_share)
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui svy: mean `var' 
	local un`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui svy: mean `var' 
	local un`i'=_b[`var']
	local i=`i'+1
	}	
	
	local un_se=0
	forvalue i=1/11{
	local un_se=`se`i''+`un_se'
	}
	local un_se=`un_se'/11
	
	* Stratified + weighted
	
	svyset _n [pw=weight_adult], poststrata(mi01) postweight(region_share)
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui svy: mean `var' 
	local x`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	svyset _n [pw=weight_hh], poststrata(mi01) postweight(region_share)

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui svy: mean `var' 
	local x`i'=_b[`var']
	local i=`i'+1
	}
	
	local x_se=0
	forvalue i=1/11{
	local x_se=`se`i''+`x_se'
	}
	local x_se=`x_se'/11
	
	* Not imputed
	
	svyset _n [pw=weight_adult2], poststrata(mi01) postweight(region_share)
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui svy: mean `var' 
	local i`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	svyset _n [pw=weight_hh2], poststrata(mi01) postweight(region_share)

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui svy: mean `var' 
	local i`i'=_b[`var']
	local i=`i'+1
	}	
	
	local i_se=0
	forvalue i=1/11{
	local i_se=`se`i''+`i_se'
	}
	local i_se=`i_se'/11
	
	* Regions grouped
	
	svyset _n [pw=weight_adult], poststrata(reg_group) postweight(region_share2)
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui svy: mean `var' 
	local rg`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	svyset _n [pw=weight_hh], poststrata(reg_group) postweight(region_share2)

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui svy: mean `var' 
	local rg`i'=_b[`var']
	local i=`i'+1
	}	
	
	local rg_se=0
	forvalue i=1/11{
	local rg_se=`se`i''+`rg_se'
	}
	local rg_se=`rg_se'/11
	
		* Regions grouped + sex
	
	svyset _n [pw=weight_adult], poststrata(strata_reg_sex2) postweight(reg_sex_share2)
	
	local i=1
	foreach var of varlist ls02	age_male age_female educ_male* educ_female*{
	qui svy: mean `var' 
	local rgs`i'=_b[`var']
	local se`i'=_se[`var']
	local i=`i'+1
	}		

	svyset _n [pw=weight_hh], poststrata(strata_reg_sex2) postweight(reg_sex_share2)

	local i=12
	foreach var of varlist region1-region14 capital pov*{
	qui svy: mean `var' 
	local rgs`i'=_b[`var']
	local i=`i'+1
	}	
	
	local rgs_se=0
	forvalue i=1/11{
	local rgs_se=`se`i''+`rgs_se'
	}
	local rgs_se=`rgs_se'/11
	
	clear
	set obs 30
	
	gen var1=""
	local i=1
	foreach x in "Female share" "Male age" "Female age"  "Male no education" "Male primary education" "Male secondary education" "Male tertiary education" "Female no education" "Female primary education" "Female secondary education" "Female tertiary education" "Dakar" "Ziguinchor" "Diourbel" "Saint-Louis" "Tambacounda" "Kaolack" "Thies" "Louga" "Fatick" "Kolda" "Matam" "Kaffrine" "Kedougou" "Sedhiou" "Live in regional capital city" "Poverty $1.9 (2011)" "Poverty $3.2 (2011)" "Poverty $5.5 (2011)" "Mean SE"{
	replace var1="`x'" if _n==`i'
	local i=`i'+1
	}
	
	gen var3=""
	local i=1
	foreach x in "51.4" "36.6" "37.1"  "49.6" "19.2" "23.8" "7.4" "60" "18.2" "18" "3.8" "23.1" "4.1" "11.1" "6.6" "5.2" "7.1" "13" "6.4" "5.3" "4.9" "4.3" "4.3" "1.1" "3.4" "19.6" "38""68" "88"{
	replace var3="`x'" if _n==`i'
	local i=`i'+1
	}
	
	gen var2=""
	local i=1
	forvalue i=1/29{
	replace var2="`b`i''" if _n==`i'
	local i=`i'+1
	}
	replace var2="`b_se'" if _n==30
	
	
	gen var4=""
	local i=1
	forvalue i=1/29{
	replace var4="`x`i''" if _n==`i'
	local i=`i'+1
	}
	replace var4="`x_se'" if _n==30

	
	gen var5=""
	local i=1
	forvalue i=1/29{
	replace var5="`un`i''" if _n==`i'
	local i=`i'+1
	}
	replace var5="`un_se'" if _n==30

	
	gen var6=""
	local i=1
	forvalue i=1/29{
	replace var6="`i`i''" if _n==`i'
	local i=`i'+1
	}
	replace var6="`i_se'" if _n==30

	gen var7=""
	local i=1
	forvalue i=1/29{
	replace var7="`rg`i''" if _n==`i'
	local i=`i'+1
	}
	replace var7="`rg_se'" if _n==30
	
	gen var8=""
	local i=1
	forvalue i=1/29{
	replace var8="`rgs`i''" if _n==`i'
	local i=`i'+1
	}
	replace var8="`rgs_se'" if _n==30

	replace var2=substr(var2,1, 4)
	replace var4=substr(var4,1, 4)
	replace var5=substr(var5,1, 4)
	replace var6=substr(var6,1, 4)
	replace var7=substr(var7,1, 4)
	replace var8=substr(var8,1, 4)

	texsave  var1 var2 var5 var4 var6 var7 var8 var3 using "$root/tex/table2",nonames replace frag title ("Sample representativeness") nofix headerlines("Variables & Unweighted & Stratified & Weighted + Stratified & No imputaion & Regions grouped & RG +sex & National statistics ") 
	
		texsave  var1 var2 var4 var3 using "$root/tex/sample",nonames replace frag title ("Sample representativeness") nofix headerlines("Variables & Unweighted & Weighted + Stratified & National statistics ") footnote ("Source of data for national statistics: \begin{itemize} \item ANSD, 2018: female share, female/male age and regions \item DHS, 2017: Male/female education \item RGPHAE, 2013: Live in a regional capital \item World Bank, 2011: Poverty rates \end{itemize}")



